package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 合同查询
 * 
 */
public class SearchContract extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar1 = null;

	Button search = new Button("查询");// 查询按钮
	Button export =  new Button("导出报表");
	private String contract_begin = "0";
	private String contract_end = "0";

	String sql = "select employinnertitle,hr_bcode_contract.contract_begin_date,hr_bcode_contract.contract_end_date,bcode_job.employno,bcode.bname,bcode.bird,up_department,bcode.departments,bcode.hr_firstname_en,bcode_job.entrydate from bcode inner join bcode_job on bcode.bcode = bcode_job.bcode left join hr_bcode_contract on bcode.bcode = hr_bcode_contract.bcode where employstatus = '在职' and jobtype != '临时工'";
	int model = 0;

	//页面通过菜单打开的默认SQL语句

	public SearchContract() {
		init();
	}
	
	public SearchContract(String sql) {
		sql = sql.replaceAll("count.*num", "signed_date,employinnertitle,hr_bcode_contract.contract_begin_date,hr_bcode_contract.contract_end_date,bcode_job.employno,bcode.bname,bcode.bird,up_department,bcode.departments,bcode.hr_firstname_en,bcode_job.entrydate");  //将传过来的查询个数的SQL语句改成查询所有在查询范围内的合同
		this.sql = sql + " order by signed_date asc";  //页面不是通过菜单打开的情况下的SQL语句
		if (sql.indexOf("contract_end_date < getdate()") > -1) {
			model = 2;
		} else {
			model = 1;
		}
		init();
	}

	public void complete() {
		super.complete();
		getToolbarll().add(export);
		getToolbarll().add(print);
		print.addListener(Events.OnClick, getListener());
		export.addListener(Events.Select, getListener());
	}
	public void init() {
		initWeigetx();
		registerMainStore(store);
		search.addListener(Events.OnClick, getListener());
	}

	private void initWeigetx() {
		toolBar1 = new PagingToolBar(15);

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		FieldSet fs = new FieldSet();
		fs.setHeading("合同详情查询");

		fs.add(createForm());

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(fs);
		vp.add(panel2);
		add(vp);
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("employno");
		type.getField("employno").setType(String.class);
		type.addField("departments");
		type.getField("departments").setType(String.class);
		type.addField("up_department");
		type.getField("up_department").setType(String.class);
		type.addField("employinnertitle");
		type.getField("employinnertitle").setType(String.class);
		type.addField("hr_firstname_en");
		type.getField("hr_firstname_en").setType(String.class);
	
		type.addField("contract_begin_date");
		type.getField("contract_begin_date").setType(Date.class);
		type.addField("contract_end_date");
		type.getField("contract_end_date").setType(Date.class);
		type.addField("bird");
		type.getField("bird").setType(Date.class);
		type.addField("entrydate");
		type.getField("entrydate").setType(Date.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_bcode_contract");
		info.setKeyname(new String[] { "icode" });
		
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("bname", "姓名", 120);
		columns.add(C1);
		
		ColumnConfig C0 = new ColumnConfig("employno", "雇佣编号", 80);  
		columns.add(C0);
		
		ColumnConfig C2 = new ColumnConfig("up_department", "部门", 80);  
		BaseComboBoxForm<BusiObject> bf1 = new BaseComboBoxForm<BusiObject>("id","name","SystemInfo","",true);
		C2.setRenderer(bf1);
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("departments", "分部", 80);
		columns.add(C3);
		
		ColumnConfig C4 = new ColumnConfig("hr_firstname_en", "英文名", 80);
		columns.add(C4);
		
		ColumnConfig C5 = new ColumnConfig("bird", "出生日期", 80);
		C5.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C5);
		ColumnConfig C9 = new ColumnConfig("employinnertitle", "职位", 120);
		columns.add(C9);
		
		ColumnConfig C8 = new ColumnConfig("entrydate", "入职日期", 90);
	    C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C8);
		
		ColumnConfig C6 = new ColumnConfig("contract_begin_date", "合同开始日", 90);
	    C6.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C6);
		
		ColumnConfig C7 = new ColumnConfig("contract_end_date", "合同到期日", 90);
	    C7.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C7);
		

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		Tools.setResize(grid, 0.99, 0.68);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.getStore().reload();  //暂时先用reload，因为不reload的话，分面不出来效果，但是这样数据多了会慢。再想解决方法
		return grid;
	}

	FormPanel form;
	DateField effectiveEnd = new DateField(); // 有效结束日期
	DateField effectiveEnd_end = new DateField(); // 有效结束日期

	private FormPanel createForm() {
		form = new FormPanel();

		effectiveEnd.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		effectiveEnd_end.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));

		TableLayout tl = new TableLayout(8);

		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.add(new Label("终止日期从:"), td);
		form.add(effectiveEnd, td);

		form.add(new Label("到:"), td);
		form.add(effectiveEnd_end, td);
		
		form.add(search);

		form.setLayout(tl);

		return form;
	}

	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				String searchSql = "";
				
				if (sql.indexOf("order by") >= 0) {
					searchSql = sql.substring(0,sql.length() - 25);
				} else {
					searchSql = sql;
				}
				
				String where = "";
				if(be.getSource().equals(search)){
				if (!effectiveEnd.getRawValue().equals("")) {
					contract_begin = effectiveEnd.getRawValue();
					where = where + " and contract_end_date >= '" + effectiveEnd.getRawValue() + "'";
				}else{
					contract_begin = "0";
				}
				if (!effectiveEnd_end.getRawValue().equals("")) {
					contract_end = effectiveEnd_end.getRawValue();
					where = where + " and contract_end_date <= '" + effectiveEnd_end.getRawValue() + "'";
				}else{
					contract_end = "0";
				}
				searchSql += where;
				grid.getStore().getStoreinfo().setSql(searchSql);
				grid.getStore().reload();
			}else{
				
				StringBuilder data = new StringBuilder();
				data.append("&contract_begin=");
				data.append(contract_begin);
				data.append("&contract_end=");
				data.append(contract_end);
				data.append("&model=");
				data.append(model);
				
				if (be.getSource().equals(export)) {
					data.append("&excel=1");
				}
		
		Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/SearchContract.jsp?" + URL.encode(data.toString()), "_blank", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
}
	}
	
	};
	
		return defaultlistener;
	}
}
